/**
 * Wechat v1.0.0
 * @Class Wechat
 * @description shopro-wechat 1.0.0 wehcat第三方登录组件
 * @Author llidongtony
 * @Date 2020-02-19
 * @Email lidongtony@qq.com
 */
import api from '@/anaf/request/index';
import $platform from '@/anaf/platform';
import store from '@/anaf/store';
import user from '@/anaf/store';

import {
	router
} from '@/anaf/router';
import {
	API_URL
} from '@/env'


export default {
	eventMap(event) {
		let map = '';
		switch (event) {
			case 'login':
				map = '登录中...';
				break;
			case 'refresh':
				map = '更新中...';
				break;
			case 'bind':
				map = '绑定中...';
				break;
		}
		return map;
	},


	async login(wxcode) {
		let token = '';
		// #ifdef MP-WEIXIN
		token = await this.wxMiniProgramOauth('login');
		return token;
		// #endif
		// #ifdef H5
		this.wxOfficialAccountOauth('login');
		// #endif
		// #ifdef APP-PLUS
		token = await this.wxOpenPlatformOauth('login');
		return token;
		// #endif
	},
	async refresh() {
		let token = '';
		// #ifdef MP-WEIXIN
		token = await this.wxMiniProgramOauth('refresh');
		return token;
		// #endif
		// #ifdef H5
		this.wxOfficialAccountOauth('refresh');
		// #endif

		// #ifdef APP-PLUS
		token = await this.wxOpenPlatformOauth('refresh');
		return token;
		// #endif
	},
	async bind() {
		let token = '';
		// #ifdef MP-WEIXIN
		token = await this.wxMiniProgramOauth('bind');
		return token;
		// #endif
		// #ifdef H5
		this.wxOfficialAccountOauth('bind');
		// #endif
		// #ifdef APP-PLUS
		token = await this.wxOpenPlatformOauth('bind');
		return token;
		// #endif
	},



	// #ifdef MP-WEIXIN
	// 微信小程序静默登录
	getWxMiniProgramSessionKey(autoLogin = true) {
		let that = this;
		let sessionStatus = false;
		let session_key = '';
		return new Promise((resolve, reject) => {
			uni.checkSession({
				success(res) {
					if (res.errMsg === 'checkSession:ok') sessionStatus = true;
				},
				complete() {
					if (uni.getStorageSync('session_key') && sessionStatus) {
						resolve(uni.getStorageSync('session_key'));
					} else {
						uni.login({
							success: function (info) {
								let code = info.code;
								api('user.getWxMiniProgramSessionKey', {
									code: code,
									autoLogin: autoLogin
								}).then(res => {
									if (res.code === 1) {
										uni.setStorageSync('session_key', res
											.data.session_key);
										if (autoLogin) {
											if (res.data.token) {

												resolve(res.data.token);
											} else {
												resolve(false);
											}
										}
										resolve(res.data.session_key);
									} else {
										reject(res.msg);
									}
								});
							}
						});
					}
				}
			})
		});
	},
	// #endif

	// 微信小程序获取用户信息登录
	async wxMiniProgramOauth(event = 'login') {
		let that = this;


		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: that.eventMap("login")
			})
			uni.getUserProfile({
				desc: '完善会员资料',
				success: async infoRes => {
					if (infoRes.errMsg === "getUserProfile:ok") {
						uni.login({
							success: async login_res => {
								if (login_res.code) {
									api('user.wxMiniProgramOauth', {
										encryptedData: infoRes.encryptedData,
										iv: infoRes.iv,
										code: login_res.code,
										signature: infoRes.signature,
									}).then(res => {
										if (res.status == 200) {
											resolve(res.token);
										} else {
											uni.removeStorageSync('session_key');
											resolve(false);
										}
									});
								}
							}, complete: res => {
								uni.hideLoading();
							}
						});
					}
				}, complete: res => {
					uni.hideLoading();
				}
			});
		});




		// let that = this
		// let fix = 1
		// await uni.getUserProfile({ // 必须手动确认触发
		// 	desc: '登录及注册信息完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
		// 	success: res => {
		// 		let oauth_data = {
		// 			iv:res.iv,
		// 			signature:res.signature,
		// 			code:wxcode,
		// 			encryptedData: res.encryptedData,
		// 		}
		// 		this.to_login(oauth_data)

		// 	}
		// })
		// setTimeout(() => console.log("暂停1秒"), 1000)



	},

	getUserProfile(wxcode) {

	},



	async to_login(data) {
		console.log(data)
		await api('user.wxMiniProgramOauth', {
			encryptedData: data.encryptedData,
			iv: data.iv,
			code: data.code,
			signature: data.signature,
		}).then(res => {
			if (res.status == 200) {
				store.token = res.token
			} else {
				uni.removeStorageSync('session_key');
				return "";
			}
		});

	}





}
